package cn.nawang.ebeim.client.version;

/**
 * 38. Count and Say My Submissions Question
 Total Accepted: 72109 Total Submissions: 257130 Difficulty: Easy
 The count-and-say sequence is the sequence of integers beginning as follows:
 1, 11, 21, 1211, 111221, ...

 1 is read off as "one 1" or 11.
 11 is read off as "two 1s" or 21.
 21 is read off as "one 2, then one 1" or 1211.
 Given an integer n, generate the nth sequence.

 Note: The sequence of integers will be represented as a string.

 * Created by GanJc on 2016/2/24.
 */
public class CountAndSay {

    public static void main(String[] args) {
        System.out.println(countAndSay(10));
    }

    public static String countAndSay(int n) {
        String result = "1";
        for (int i = 1; i < n; i++) {
            System.out.print(result + ",");
            result = countAndSay(result);
        }
        return result;
    }

    public static String countAndSay(String str){
        String result = "";
        char temp = str.charAt(0);
        int count = 1 ;
        for (int i = 1; i < str.length() ; i++) {
            if (temp == str.charAt(i)) {
                count++;
                continue;
            }else {
                result = result + count +temp ;
                count = 1;
                temp = str.charAt(i);
            }
        }
        result = result + count +temp ;
        return result ;
    }



}
